// WndPbSpeed.cpp : implementation file
//

#include "stdafx.h"
#include "BBViewer.h"
#include "WndPbSpeed.h"


// CWndPbSpeed

IMPLEMENT_DYNAMIC(CWndPbSpeed, CWnd)

CWndPbSpeed::CWndPbSpeed()
{
	m_nPbSpeed = 2;

}

CWndPbSpeed::~CWndPbSpeed()
{
}


BEGIN_MESSAGE_MAP(CWndPbSpeed, CWnd)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_MESSAGE(WM_CTL_MESSAGE, OnCtlMessage)
END_MESSAGE_MAP()



// CWndPbSpeed message handlers

int CWndPbSpeed::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	LPST_WND_POS lpWndPos = theApp.WndPos.GetAt(wpPBSpeed);
	m_nMode = lpWndPos->nMode;
	if (m_nMode == 0)
	{
		m_rcSpeed[0].left   = 5;
		m_rcSpeed[0].right  = 20;
		m_rcSpeed[0].top    = 33;
		m_rcSpeed[0].bottom = 47;

		m_rcSpeed[1].left   = 13;
		m_rcSpeed[1].right  = 29;
		m_rcSpeed[1].top    = 17;
		m_rcSpeed[1].bottom = 32;

		m_rcSpeed[2].left   = 32;
		m_rcSpeed[2].right  = 62;
		m_rcSpeed[2].top    = 5;
		m_rcSpeed[2].bottom = 19;

		m_rcSpeed[3].left   = 63;
		m_rcSpeed[3].right  = 77;
		m_rcSpeed[3].top    = 17;
		m_rcSpeed[3].bottom = 32;

		m_rcSpeed[4].left   = 74;
		m_rcSpeed[4].right  = 89;
		m_rcSpeed[4].top    = 33;
		m_rcSpeed[4].bottom = 47;
	}
	else if (m_nMode == 1)
	{
		int i;
		LPST_BMP_INFO lpBmpInfo;
		for (i=0;i<4;i++)
		{
			lpBmpInfo = theApp.BmpInfo.GetAt(biPB_slower_normal+i);
			m_btnSlower.SetBitmaps(i, lpBmpInfo->hbmpImage);

			lpBmpInfo = theApp.BmpInfo.GetAt(biPB_faster_normal+i);
			m_btnFaster.SetBitmaps(i, lpBmpInfo->hbmpImage);
		}
	}

	//	LPST_BMP_INFO
	if (m_nMode == 1)
	{
		int i;
		int nBtnCount = 0;

		LPST_BMP_INFO lpBmpInfo;
		CCtrlButton *pBtn[4];
		pBtn[nBtnCount++] = &m_btnSlower;
		pBtn[nBtnCount++] = &m_btnFaster;

		for (i=0;i<nBtnCount;i++)
		{
			lpBmpInfo = theApp.BmpInfo.GetAt(biPB_slower_normal+(i*4));
			if (!pBtn[i]->Create(NULL, NULL, WS_CHILD|WS_VISIBLE, CRect(0,0,0,0), this, lpBmpInfo->uImageID, NULL))
				return -1;
		}
	}
	return 0;
}

void CWndPbSpeed::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);

	if (m_nMode == 1)
	{
		LPST_WND_POS lpWndPos;
		lpWndPos = theApp.WndPos.GetAt(wpPBSpeed_slower);
		theApp.SetWindowPosition(&m_btnSlower, cx, cy, lpWndPos->rtWndPos, lpWndPos->rtStickTo);
		lpWndPos = theApp.WndPos.GetAt(wpPBSpeed_faster);
		theApp.SetWindowPosition(&m_btnFaster, cx, cy, lpWndPos->rtWndPos, lpWndPos->rtStickTo);
	}
}

void CWndPbSpeed::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	// Do not call CWnd::OnPaint() for painting messages
	UpdatePbSpeed();

}

void CWndPbSpeed::OnLButtonDown(UINT nFlags, CPoint point)
{
	int i;
	if (m_nMode == 0)
	{
		for(i=0;i<5; i++)
		{
			if (PtInRect(&m_rcSpeed[i], point))
			{
				LPST_BMP_INFO lpBmpInfo;
				lpBmpInfo = theApp.BmpInfo.GetAt(biPBSpeed_bk);
				m_nPbSpeed = i;
				CWnd* pParent = GetParent();
				pParent->PostMessage(WM_PLB_MESSAGE, lpBmpInfo->uImageID, m_nPbSpeed);
				UpdatePbSpeed();
				break;
			}
		}
	}

	CWnd::OnLButtonDown(nFlags, point);
}

void CWndPbSpeed::SetPbSpeed(int nPbSpeed)
{
	m_nPbSpeed = nPbSpeed;

	UpdatePbSpeed();
}

void CWndPbSpeed::UpdatePbSpeed()
{
	CDC* pDC = GetDC();

	//------------------------------------------
	CDC dcMemory;
	LPST_WND_POS lpWndPos;
	dcMemory.CreateCompatibleDC(pDC);

	CBitmap *pBmp, *pBmpOld;
	LPST_BMP_INFO lpBmpInfo;
	lpBmpInfo = theApp.BmpInfo.GetAt(biPBSpeed_bk);
	pBmp = CBitmap::FromHandle(lpBmpInfo->hbmpImage);
	pBmpOld = dcMemory.SelectObject(pBmp);


	//------------------------------------------
	CDC dcSpeed;
	dcSpeed.CreateCompatibleDC(pDC);


	CBitmap *pBmpSpeedOld, *pBmpSpeed;
	lpBmpInfo = theApp.BmpInfo.GetAt(biPBSpeed_x1_4+m_nPbSpeed);
	pBmpSpeed = CBitmap::FromHandle(lpBmpInfo->hbmpImage);
	pBmpSpeedOld = dcSpeed.SelectObject(pBmpSpeed);

	
	//------------------------------------------
	lpWndPos = theApp.WndPos.GetAt(wpPBSpeed_stat);
	dcMemory.BitBlt(lpWndPos->rtWndPos.left, lpWndPos->rtWndPos.top, lpWndPos->rtWndPos.Width(), lpWndPos->rtWndPos.Height(), &dcSpeed, 0, 0, SRCCOPY);

	lpWndPos = theApp.WndPos.GetAt(wpPBSpeed_bk);
	pDC->BitBlt(lpWndPos->rtWndPos.left, lpWndPos->rtWndPos.top, lpWndPos->rtWndPos.Width(), lpWndPos->rtWndPos.Height(), &dcMemory, 0, 0, SRCCOPY);

	dcSpeed.SelectObject(pBmpSpeedOld);
	dcMemory.SelectObject(pBmpOld);	

	dcSpeed.DeleteDC();		//2013.04.29
	dcMemory.DeleteDC();	//2013.04.29

	ReleaseDC(pDC);
}

long CWndPbSpeed::OnCtlMessage(UINT wParam, LONG lParam)
{
	LPST_BMP_INFO lpBmpInfo, lpInfo;
	CWnd* pParent = GetParent();

	lpBmpInfo = theApp.BmpInfo.GetAt(biPBSpeed_bk);

	lpInfo = theApp.BmpInfo.GetAt(biPB_slower_normal);
	if (lpInfo->uImageID == (int)wParam)
	{
		if (m_nPbSpeed>0)
		{
			m_nPbSpeed--;
			pParent->PostMessage(WM_PLB_MESSAGE, lpBmpInfo->uImageID, m_nPbSpeed);
			UpdatePbSpeed();
		}
		return 0;
	}

	lpInfo = theApp.BmpInfo.GetAt(biPB_faster_normal);
	if (lpInfo->uImageID == (int)wParam)
	{
		if (m_nPbSpeed<4)
		{
			m_nPbSpeed++;
			pParent->PostMessage(WM_PLB_MESSAGE, lpBmpInfo->uImageID, m_nPbSpeed);
			UpdatePbSpeed();
		}
		return 0;
	}
	return 0;
}

